package com.secsbrain.cmdb.web.controller;

import java.util.Date;

import com.secsbrain.sso.core.SsoHelper;
import com.secsbrain.cmdb.dal.model.DeployDiagram;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.secsbrain.cmdb.biz.IDeployDiagramService;
import com.secsbrain.common.ResponseResult;

/**
 * 部署图控制层
 * @author zhaoyi
 *
 */
@RestController
@RequestMapping("api/deployDiagram")
public class DeployDiagramController{
	
    @Autowired
    IDeployDiagramService deployDiagramService;
    
    /**
   	 * @api {GET} /api/deployDiagram/query  根据集群查询部署图数据格式
   	 * 
   	 * @apiGroup HOST
   	 * 
   	 * @apiParam {String{5}}  clusterName  集群
   	 * 
   	 * @apiSuccess {String} data          部署图数据json字符串
     */
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseResult<?> query(String clusterName) {
		Assert.hasText(clusterName, "clusterName不能为空");
		
		DeployDiagram deployDiagram=new DeployDiagram();
		deployDiagram.setClusterName(clusterName);
		DeployDiagram result=deployDiagramService.selectOne(deployDiagram);
		if(result==null){
	    	return new ResponseResult();
		}else{
	    	return new ResponseResult(result.getModelJson());
		}
    }
    
    /**
   	 * @api {PUT} /api/deployDiagram/save  保存集群查询部署图
   	 * 
   	 * @apiGroup HOST
   	 * 
   	 * @apiParam {String{5}}  clusterName  集群
   	 * @apiParam {TEXT}       modelJson    部署图数据json字符串
   	 * 
     */
    @RequestMapping(value = "/save", method = RequestMethod.PUT)
    public ResponseResult<?> save(@RequestBody DeployDiagram deployDiagram) {
		Assert.hasText(deployDiagram.getClusterName(), "clusterName不能为空");
    	
    	DeployDiagram deployDiagramParam=new DeployDiagram();
    	deployDiagramParam.setClusterName(deployDiagram.getClusterName());
		DeployDiagram result=deployDiagramService.selectOne(deployDiagramParam);
		if(result==null){
			DeployDiagram deployDiagramInsert=new DeployDiagram();
			deployDiagramInsert.setClusterName(deployDiagram.getClusterName());
			deployDiagramInsert.setCreateTime(new Date());
			deployDiagramInsert.setCreator(SsoHelper.currentUser().getNickname());
			deployDiagramInsert.setModelJson(deployDiagram.getModelJson());
			deployDiagramService.insertSelective(deployDiagramInsert);
		}else{
			DeployDiagram deployDiagramUp=new DeployDiagram();
			deployDiagramUp.setUpdateTime(new Date());
			deployDiagramUp.setUpdator(SsoHelper.currentUser().getNickname());
			deployDiagramUp.setModelJson(deployDiagram.getModelJson());

			DeployDiagram whereEntity=new DeployDiagram();
			whereEntity.setClusterName(deployDiagram.getClusterName());
			deployDiagramService.updateSelective(deployDiagramUp, whereEntity);
		}
		
    	return new ResponseResult();
    }
    
}
